#include "log.h"

#include "spdlog/spdlog.h"
#include "spdlog/sinks/daily_file_sink.h"
#include "spdlog/sinks/stdout_color_sinks.h"
#include "spdlog/sinks/basic_file_sink.h"
#include "spdlog/async.h"


Log::Log(){}
Log::~Log(){}


Log& Log::GetInstance(){
    static Log instance;
    return instance;
}

void Log::Init(){
    if(logger_){
        return;
    }

    // 默认配置：控制台 + 文件
    auto console_sink = std::make_shared<spdlog::sinks::stdout_color_sink_mt>();
    auto file_sink = std::make_shared<spdlog::sinks::daily_file_sink_mt>("logs/app.log", 0, 0);

    // 创建日志器（异步模式）
    // spdlog::init_thread_pool(8192, 1);
    // logger_ = std::make_shared<spdlog::async_logger>(
    //     "main_logger",
    //     spdlog::sinks_init_list{console_sink, file_sink},
    //     spdlog::thread_pool(),
    //     spdlog::async_overflow_policy::block
    // );
    std::vector<spdlog::sink_ptr> sinks;
    sinks.push_back(console_sink);
    sinks.push_back(file_sink);
    logger_ = std::make_shared<spdlog::logger>("multi_sink", sinks.begin(), sinks.end());

    // 默认格式：[时间] [级别] [线程] 消息
    logger_->set_pattern("[%Y-%m-%d %H:%M:%S.%e] [%n] [%^%l%$] [thread %t] (%s:%#) %!(): %v");
    logger_->set_level(spdlog::level::debug); // 默认级别
    spdlog::register_logger(logger_);
    spdlog::flush_every(std::chrono::seconds(1)); // 定期刷新
}

void Log::SetLevel(spdlog::level::level_enum level){
    level_ = level;
    logger_->set_level(level);
}

